package com.sgb.member.view_model;

import androidx.lifecycle.MutableLiveData;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.CompanyInfoEntity;
import com.sgb.member.entity.CompanyNatureEntity;
import com.sgb.member.entity.MemberAddClientBean;
import com.sgb.member.entity.MemberCompanyInfoEntity;
import com.sgb.member.ui.activity.MemberAddClientActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.List;

import retrofit2.Call;

/**
 * Time:2021/8/26
 * AUTHOR:F_Q
 * Description:
 */
public class AddClientModel  extends BaseViewModel {
    MemberAddClientActivity activity;
    MemberRepertory repertory;

    public MutableLiveData<Boolean> loadCompanyData = new MutableLiveData<>();

    public AddClientModel(MemberAddClientActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(activity, "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    /**
     * 模糊查询公司信息
     * @param name
     */
    public void getCompanyName(String name) {
        loadCompanyData.setValue(true);
        repertory.getApi().getCompanyInfoLikeName(name).enqueue(new ICallBack<BaseEntity<List<MemberCompanyInfoEntity>>>() {

            @Override
            public void onSuccess(BaseEntity<List<MemberCompanyInfoEntity>> data) {
                loadCompanyData.setValue(true);
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setCompanyNameList(data.getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MemberCompanyInfoEntity>>> call, Throwable t) {
                super.onFailure(call, t);
                loadCompanyData.setValue(false);
            }
        });
    }

    /**
     * 根据企业性质查询
     */
    public void getCompanyNatureList() {
        showOrHideDialog(true);
        repertory.getApi().getCompanyQualityConfig().enqueue(new ICallBack<BaseEntity<List<CompanyNatureEntity>>>() {

            @Override
            public void onSuccess(BaseEntity<List<CompanyNatureEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setCompanyNatureList(data.getData());
                }
            }
        });
    }
    //行业类型
    public void getIndustryTypeList() {
        showOrHideDialog(true);
        repertory.getApi().getIndustryCategory().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setIndustryTypeList(data.getData());
                }
            }
        });

    }

    /**
     * 根据筛选的no来查询当前选择的企业信息
     * @param companyNo
     */
    public void getDetailsByCompany(String companyNo) {
        showOrHideDialog(true);
       repertory.getApi().getDetailsByCompanyNo(companyNo).enqueue(new ICallBack<BaseEntity<CompanyInfoEntity>>() {
           @Override
           public void onSuccess(BaseEntity<CompanyInfoEntity> data) {
               if (data.getData() != null ){
                   activity.setDetailsByCompany(data.getData());
               }
           }
       });
    }

    public void getsaveOrUpdate(MemberAddClientBean bean) {
        showOrHideDialog(true);
        repertory.getApi().getsaveOrUpdate(bean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                activity.onSuccess();
            }
        });
    }
}
